package com.spzx.test1.t9;
/*
    并非全力
*/

import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomUtils;
import org.apache.poi.hssf.record.DVALRecord;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Screen {

    Object content = null;
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    @SneakyThrows
    public void writeContent(Object o) {
        lock.writeLock().lock();
        System.out.println(Thread.currentThread().getName() + "写入屏幕");
        this.content = o;
        TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1, 5));
        System.out.println(Thread.currentThread().getName() + "写入完成" + content);
        lock.writeLock().unlock();
    }


    @SneakyThrows
    public Object readContent() {
        lock.readLock().lock();
        System.out.println(Thread.currentThread().getName() + "读取屏幕");
        TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1, 5));
        System.out.println(Thread.currentThread().getName() + "读取完成");
        lock.readLock().unlock();
        return content;
    }
}
